<?php

class UserBean implements IDBBean {
	
	public $id;
	public $type;
	public $username;
	public $name;
	public $password;
	public $department;
	public $phone;
	public $email;
	public $fax;
	
	public static $_ADMIN = 1;
	public static $_INSTITUE = 2;
	public static $_SENATE = 3;
	public static $_GUEST = "*";
	/**
	 * @return the $username
	 */
	public function getUsername() {
		return $this->username;
	}
	
	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels() {
		return array ( );
	}

	/**
	 * @param field_type $username
	 */
	public function setUsername($username) {
		$this->username = $username;
	}

	public function __construct() {
	
	}
	
	/**
	 * @return the $id
	 */
	public function getId() {
		return $this->id;
	}
	
	/**
	 * @return the $type
	 */
	public function getType() {
		return $this->type;
	}
	
	/**
	 * @return the $name
	 */
	public function getName() {
		return $this->name;
	}
	
	/**
	 * @return the $password
	 */
	public function getPassword() {
		return $this->password;
	}
	
	public static function findUserById($id){
		$sql = "select * from user where id='$id'";
		$db = MysqliObj::getInstance ();
		$data = $db->fetch_array ( $sql );
		if (! $data)
			return null;
		$user = new UserBean();
		foreach ($data as $key => $value){
			if(property_exists("UserBean", $key))
				$user->$key = $value;
		}
		return $user;
	}
	public static function findUserByUsername(){
	
	}
	public static function findUserByType(){
		
	}
	public static function findUserByName(){
	
	}
	
	/**
	 * @return the $department
	 */
	public function getDepartment() {
		return $this->department;
	}
	
	/**
	 * @return the $phone
	 */
	public function getPhone() {
		return $this->phone;
	}
	
	/**
	 * @return the $email
	 */
	public function getEmail() {
		return $this->email;
	}
	
	/**
	 * @return the $fax
	 */
	public function getFax() {
		return $this->fax;
	}
	
	/**
	 * @param field_type $id
	 */
	public function setId($id) {
		$this->id = $id;
	}
	
	/**
	 * @param field_type $type
	 */
	public function setType($type) {
		$this->type = $type;
	}
	
	/**
	 * @param field_type $name
	 */
	public function setName($name) {
		$this->name = $name;
	}
	
	/**
	 * @param field_type $password
	 */
	public function setPassword($password) {
		$this->password = $password;
	}
	
	/**
	 * @param field_type $department
	 */
	public function setDepartment($department) {
		$this->department = $department;
	}
	
	/**
	 * @param field_type $phone
	 */
	public function setPhone($phone) {
		$this->phone = $phone;
	}
	
	/**
	 * @param field_type $email
	 */
	public function setEmail($email) {
		$this->email = $email;
	}
	
	/**
	 * @param field_type $fax
	 */
	public function setFax($fax) {
		$this->fax = $fax;
	}
	
	public function updateToDatabase(){
		if(isset($this->id)){
			$sql = "update user set username=$this->username, password=$this->password, name=$this->name, department = $this->department, phone = $this->phone, email = $this->email, fax= $this->fax,type = $this->type 
			where id = $this->id";
		}else{
			$sql = "INSERT INTO `coursesystem`.`user` (`id`, `username`, `password`, `name`, `department`, `type`, `email`, `phone`, `fax`) 
			VALUES (NULL,'$this->username','$this->password','$this->name','$this->department','$this->type','$this->email','$this->phone','$this->fax')";
		}
		$dbms = MysqliObj::getInstance ();
		$dbms->real_escape_string ( $sql );
		$result = $dbms->query ( $sql );
		if ($result) {
			return true;
		} else {
			return false;
		}
		return $data;
	}
}

?>